package com.sx.jys.ui.vm

import android.content.Context
import com.blankj.utilcode.util.CollectionUtils
import com.sos.widget.app.dialog.HintDialog
import com.sx.jys.app.net.HttpRequestCoroutine
import com.sx.jys.app.net.bean.OrderBean
import com.sx.jys.app.until.MutableLiveListData
import com.sx.jys.app.until.request2
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.livedata.BooleanLiveData
import me.hgj.jetpackmvvm.callback.livedata.StringLiveData

class CkVm : BaseViewModel() {
    val listLiveData = MutableLiveListData<OrderBean>()

    val uiState = UIStateChange()

    inner class UIStateChange {
        val refreshEvent = BooleanLiveData()
        val loadMoreEvent = BooleanLiveData()
        val doDuoField = StringLiveData()
        val doNullField = StringLiveData()
        val autoRefreshEvent = BooleanLiveData()
    }

    fun reqCkList(page: Int, type: String) {
        request2({ HttpRequestCoroutine.ckList(page, type) }, {
            if (page == 1) {
                listLiveData.value = it.records
            } else {
                listLiveData.addData(it.records)
            }
            uiState.refreshEvent.postValue(true)
            uiState.loadMoreEvent.postValue(!CollectionUtils.isEmpty(it.records))
        }, isShowDialog = page == 1)

    }

    fun reqCkNum(type: String) {
        if ("0".equals(type) || "4".equals(type)) {
            //委托 //持仓中
            request2({ HttpRequestCoroutine.ckNum() }, {
                if ("4".equals(type)) {
                    //委托
                    uiState.doDuoField.value = it.entrustMany.orEmpty()
                    uiState.doNullField.value = it.entrustEmpty.orEmpty()
                } else if ("0".equals(type)) {
                    //持仓
                    uiState.doDuoField.value = it.haveMany.orEmpty()
                    uiState.doNullField.value = it.haveEmpty.orEmpty()
                }
            })
        }
    }

    fun reqCloseAll(context: Context, orderBean: OrderBean? = null) {
        request2({
            HttpRequestCoroutine.closeAll(
                orderBean?.code,
                orderBean?.openTime,
                orderBean?.isOneOrder
            )
        }, {
            uiState.autoRefreshEvent.value = true
            HintDialog.Builder(context)
                .setIcon(HintDialog.ICON_FINISH)
                .setMessage("操作成功!")
                .show()
        }, isShowDialog = true)
    }

    fun reqOrderBack(context: Context, orderBean: OrderBean? = null) {
        request2({
            HttpRequestCoroutine.orderBack(
                orderBean?.code.orEmpty(),
                orderBean?.openTime.orEmpty()
            )
        }, {
            uiState.autoRefreshEvent.value = true
            HintDialog.Builder(context)
                .setIcon(HintDialog.ICON_FINISH)
                .setMessage("撤销成功!")
                .show()
        }, isShowDialog = true)
    }

    fun setProfitloss(context: Context,orderBean: OrderBean, stopPrice: String? = "", losePrice: String? = "") {
        request2({
            HttpRequestCoroutine.setProfitloss(
                orderBean.code.orEmpty(),
                orderBean.openTime.orEmpty(),
                stopPrice,
                losePrice
            )
        }, {
            uiState.autoRefreshEvent.value = true
            HintDialog.Builder(context)
                .setIcon(HintDialog.ICON_FINISH)
                .setMessage("操作成功!")
                .show()
        }, isShowDialog = true)
    }
}